var locsSet = new Set(); // 空Set


//获得资源包信息
function getPackageInfo(){
    var tbody=window.document.getElementById("package_data");
    $.ajax({
        url:"../../package/seller",
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>"+"" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].name +"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].locsNum +"</td>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ convertStateToString(tdata[i].state) +"</td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">" +
                        "<input type='button' value='编辑' id='edit_button'+" + tdata[i].id + " onclick='editButtonClick(" + tdata[i] + ")'" +"</input>"+
                        "<input type='button' value='删除' id='del_button'+" + tdata[i].id + " onclick='deleteButtonClick( " + tdata[i].id + ")'" +"</input></tr>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function convertStateToString(state) {
    if (state == 0) {
        return "未使用";
    }
    if (state == 1) {
        return "已使用";
    }
}

function locSearchSubmit(type) {
    var fdata;
    if (type == "origin") {
        fdata = null;
    } else {
        fdata = $("#loc_search_form").serialize();
    }
    var tbody = window.document.getElementById("loc_search_data");
    $.ajax({
        url:"../../locs/search",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="";
                var tdata=data.object;
                for( i in tdata){
                    var j = parseInt(i)+1;
                    str +="<tr>" +
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\"><input type='checkbox' name='locs' value='" + tdata[i].id+ "'></span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ j +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].name +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].province + "-"+ tdata[i].city + "-"+ tdata[i].district +"</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].height +"cm</span></div></td>"+
                        "<td height=\"20\" bgcolor=\"#FFFFFF\"><div align=\"center\"><span class=\"STYLE1\">"+ tdata[i].width +"cm</span></div></td>"+
                        "</tr>";
                }
                tbody.innerHTML = str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function getProvinces() {
    var tbody=window.document.getElementById("_province");
    $.ajax({
        url:"../../loc/positions/0",
        type:"POST",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择省</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });
}

function positionLinkage() {
    $("#_province").on("change",function(){
        getCities();
        getDistricts()
    });
    $("#_city").on("change",function(){
        getDistricts()
    });

}

function getCities() {
    var tbody=window.document.getElementById("_city");
    var optionProvince=$("#_province option:selected");  //获取选中的项
    $.ajax({
        url:"../../loc/positions/1",
        type:"POST",
        data:{province:optionProvince.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择市</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function getDistricts() {
    var tbody=window.document.getElementById("_district");
    var optionProvince=$("#_province option:selected");
    var optionCity=$("#_city option:selected");
    $.ajax({
        url:"../../loc/positions/2",
        type:"POST",
        data:{province:optionProvince.val(), city:optionCity.val()},
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var str="<option value=''>选择区/县</option>";
                var tdata=data.object;
                for( i in tdata) {
                    str += "<option value='" + tdata[i] + "'>" + tdata[i] + "</option>";
                }
                tbody.innerHTML =str;
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

function addLocs() {
    var locsArray = [];
    var locsString = document.getElementById("_locIds").value;
    if (locsString != null ) {
        locsArray = locsString.split(",");
    }
    var newLocs = document.getElementsByName("locs");
    for (k in newLocs) {
        if (newLocs[k].checked) {
            if (locsArray.indexOf(newLocs[k].value.toString()) <= -1) {
                locsArray.push(newLocs[k].value.toString());
            }
        }
    }
    var str = locsArray.toString();
    if (str.charAt(0) == ',') {
        str = str.substring(1, str.length);
    }
    document.getElementById("_locIds").value = str;
}

function deleteLocs() {
    var locsArray = [];
    var locsString = document.getElementById("_locIds").value;
    if (locsString != null ) {
        locsArray = locsString.split(",");
    }
    var newLocs = document.getElementsByName("locs");
    for (k in newLocs) {
        if (newLocs[k].checked) {
            var locsArrayIndex = locsArray.indexOf(newLocs[k].value.toString());
            // 原数组中存在需要删除的元素
            if (locsArrayIndex > -1) {
                locsArray.splice(locsArrayIndex,1);
            }
        }
    }
    var str = locsArray.toString();
    if (str.charAt(0) == ',') {
        str = str.substring(1, str.length);
    }
    document.getElementById("_locIds").value = str;
}


function addButtonClick() {
    location.href="./package_add.html";
}

function addSubmitButtonClick() {
    var fdata = $("#package_add_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../package/new",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./package_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

//      删除已选设备
function deleteButtonClick(id){
    var fdata = $("#packages_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../package/deletion/" + id,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./package_list.html";
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });

}

// 编辑时，获取id号对应的设备信息，并设初值
function setOriginInfo() {
    var url = window.location.href;
    var site_id = getArgsFromHref(url, "id");
    $.ajax({
        url:"../../template/" + site_id,
        type:"GET",
        dataType:"json",
        success:function(data){
            console.log(data);
            if(data.code==0){
                var tdata=data.object;
                document.getElementById("_id").value=tdata.id;
                document.getElementById("_ownId").value=tdata.ownId;
                document.getElementById("_name").value=tdata.name;
                document.getElementById("_height").value=tdata.height;
                document.getElementById("_width").value=tdata.width;
                document.getElementById("_country").value=tdata.position.country;
                document.getElementById("_province").value=tdata.position.province;
                document.getElementById("_city").value=tdata.position.city;
                document.getElementById("_district").value=tdata.position.district;
                document.getElementById("_longitude").value=tdata.longitude;
                document.getElementById("_latitude").value=tdata.latitude;

                if (tdata.type == 0) {
                    document.getElementById("_type0").click();
                } else {
                    document.getElementById("_type1").click();
                }
            }
        },
        error:function(){
            alert("search fails！");
        }
    });

}

// 点击编辑按钮时，跳转到编辑页面
function editButtonClick(tdata){
    if (tdata.state != 0) {
        alert("资源包使用中，不可编辑！");
    }
    location.href="./dev_edit.html?id=" + id;
}

// 编辑时，点击提交按钮
function editSubmitButtonClick(){
    var fdata = $("#template_edit_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../template/edit",
        type:"POST",
        data:fdata,
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                location.href="./template_list.html";
            }else if(result.code==1){
                alert(result.message);
                return;
            }
        }
    });
}

// 获取url中的某个参数
function getArgsFromHref(sHref, sArgName)
{
    var args = sHref.split("?");
    var retval = "";
    if(args[0] == sHref) /*参数为空*/
    {
        return retval; /*无需做任何处理*/
    }
    var str = args[1];
    args = str.split("&");
    for(var i = 0; i < args.length; i ++)
    {
        str = args[i];
        var arg = str.split("=");
        if(arg.length <= 1) continue;
        if(arg[0] == sArgName) retval = arg[1];
    }
    return retval;
}

function rmResButtonClick(devId, type) {
    var fdata = $("#devs_form").serialize();
    $.param(fdata);
    $.ajax({
        url:"../../loc/deletion/dev/" + devId,
        type:"GET",
        async: false,
        contentType:"application/x-www-form-urlencoded; charset=utf-8",
        dataType:"json",
        success:function(result){
            if(result.code==0){
                alert(result.message);
                getDevInfo(type);
            }else if(result.code==1){
                alert(result.message);
            }
        }
    });
}

